package com.wonders.frame.controller;

import com.gcr.server.response.Response;
import com.wonders.frame.controller.core.CoreController;
import com.wonders.frame.entity.AuthAccount;
import com.wonders.frame.entity.AuthAccountRole;
import com.wonders.frame.entity.form.AuthAccountForm;
import com.wonders.frame.entity.form.AuthAccountRoleForm;
import com.wonders.frame.service.AuthAccountRoleService;
import com.wonders.frame.service.Core.CoreCurdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/authAccountRole")
public class AuthAccountRoleController extends CoreController<AuthAccount> {
    Logger log = LoggerFactory.getLogger(AuthRoleController.class);

    @Autowired
    private AuthAccountRoleService service;

    @Override
    protected CoreCurdService getService() {
        return service;
    }


    private final ReentrantLock lock=new ReentrantLock();


    @GetMapping("/getAccountRole")
    public Response getAccountRole(){
        try {
            log.info("request:authAccountRole/getAccountRole");
            List<AuthAccountRole> list=service.getAccountRole();
            return successResponse(list);
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }
    }

    @PostMapping("/saveAuthAccountRole")
    public Response saveAuthAccountRole(@RequestBody AuthAccountRoleForm authAccountRoleForm){
        try {
            lock.lock();
            log.info("request:authAccountRole/saveAuthAccountRole");
            log.info("AuthAccountRoleForm="+authAccountRoleForm.toString());
            service.saveAuthAccountRole(authAccountRoleForm.getAccountId(),authAccountRoleForm.getRoleIds());
            return successResponse(null);
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }finally {
            lock.unlock();
        }
    }

    @GetMapping("/getRoleByAccountId")
    public Response getRoleByAccountId(String accountId){
        try {
            return  successResponse(service.findByAccountId(accountId));
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }
    }
    //批量新增角色&用户-关系
    @RequestMapping("/batchAddRole")
    public Response saveAccountRole(@RequestBody AuthAccountForm authAccountForm){
        try {
            service.batchAddRole(authAccountForm.getAccountsId(),authAccountForm.getRoleId());
            return successResponse(null);
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }
    }

    //批量删除角色&用户-关系
    @PostMapping("/batchDeleteRole")
    public Response batchDeleteRole(@RequestBody AuthAccountForm authAccountForm){
        try {
            service.batchDeleteRole(authAccountForm.getAccountsId(),authAccountForm.getRoleId());
            return successResponse(null);
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }
    }
}
